package com.demo.connectdbtest.module.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.demo.connectdbtest.module.tool.vo.PromDataDTO;

import java.io.*;

public class JsonUtil {

    //json转对象
    public static void main(String[] args){
        String path = JsonUtil.class.getClassLoader().getResource("query_range1.json").getPath();
        String s = JsonUtil.readJsonFile(path);
        PromDataDTO parseData = JSONObject.parseObject(s, PromDataDTO.class);
        System.out.println(parseData);

    }

    /**
     * 读取json文件，返回json串
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}